package cn.laixueit.service.impl;

import cn.laixueit.common.IdWorker;
import cn.laixueit.commons.JwtUtils;
import cn.laixueit.commons.UserInfo;
import cn.laixueit.intercepter.MyIntercepter;
import cn.laixueit.mapper.OrderDetailMapper;
import cn.laixueit.mapper.OrderMapper;
import cn.laixueit.mapper.OrderStatuslMapper;
import cn.laixueit.mapper.ShipAddressMapper;
import cn.laixueit.pojo.*;
import cn.laixueit.properties.JwtProperties;
import cn.laixueit.service.ShipAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ShipAddressServiceImpl extends ServiceImpl<ShipAddressMapper, ShipAddress> implements ShipAddressService {

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderStatuslMapper orderStatuslMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public List<ShipAddress> listByUser(String token) {
        try {
            UserInfo userInfo = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
            HashMap<String, Object> map = new HashMap<>();
            map.put("uid",userInfo.getId());
            List<ShipAddress> addressList = listByMap(map).stream().collect(Collectors.toList());
            return addressList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public String createOrder(OrderVo orderVo) {
        long id = idWorker.nextId();
        Order order = new Order();
        BeanUtils.copyProperties(orderVo,order);
        List<OrderDetail> orderDetails = orderVo.getOrderDetails();
        order.setCreateTime(new Date());
        UserInfo userInfo = MyIntercepter.getThreadLocal().get();
        order.setBuyerNick(userInfo.getUsername());

        order.setOrderId(id);
        orderMapper.insert(order);

        for (OrderDetail detail : orderDetails) {
            detail.setOrderId(order.getOrderId());
            orderDetailMapper.insert(detail);
        }

        OrderStatus status = new OrderStatus();
        status.setOrderId(order.getOrderId());
        status.setStatus(1);
        status.setCreateTime(new Date());

        orderStatuslMapper.insert(status);

        return String.valueOf(order.getOrderId()) ;
    }

    @Override
    public List<OrderDetail> listOrderDetail(String orderId) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("order_id",orderId);
        List<OrderDetail> detailList = orderDetailMapper.selectByMap(map);
        return detailList;
    }

    @Override
    public void subOrder(ShipAddress shipAddress) {
        UserInfo userInfo = MyIntercepter.getThreadLocal().get();
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id",userInfo.getId());
        Order order = orderMapper.selectByMap(map).get(0);
        order.setReceiverAddress(shipAddress.getAddress());
        order.setReceiverMobile(shipAddress.getPhone());
        order.setReceiver(shipAddress.getName());
        order.setPaymentType(1);
        orderMapper.updateById(order);
    }


}
